#!/bin/bash

#=======================================================================
# create-annex-remotes
# File ID: c925d0fe-2a9a-11e4-9f76-c80aa9e67bbd
#
# Create remotes in local git-annex repositories. Example usages:
#   annex-cmd -e create-annex-remotes
#   annex-cmd -e "create-annex-remotes -r"
#
# Author: Øyvind A. Holm <sunny@sunbase.org>
# License: GNU General Public License version 2 or later.
#=======================================================================

progname=create-annex-remotes

# Set to "echo" to simulate
simul=

tmp_begin_file=/tmp/.$progname.$(date -u +"%Y%m%dT%H%M%SZ").$$.beg.tmp
tmp_end_file=/tmp/.$progname.$(date -u +"%Y%m%dT%H%M%SZ").$$.end.tmp

comp="$(hostname)"
test "$comp" = "bellmann" -o "$comp" = "linode" || {
    echo $progname: $comp: Unknown computer, should be \"bellmann\" or \"linode\" >&2
    exit 1
}

test -d .git/annex || { echo $progname: $(pwd) is not a git-annex repository >&2; exit 1; }

unset remove_remotes
test "$1" = "-r" && { remove_remotes=1; shift; }

base="$(basename $(pwd))"

if test "$base" = "annex" -a -f ".disk" -a -d ".gadu-local"; then
    is_top=1
elif test -f "../.disk" -a -d "../.gadu-local"; then
    unset is_top
else
    echo $progname: Didn\'t find .disk or .gadu-local in current or parent directory >&2
    exit 1
fi

define_remote() {
    local remote="$1"
    local url="$2"
    if test -z "$(git remote | grep "^$remote\$")"; then
        $simul git remote add $remote $url
    else
        $simul git config remote.$remote.url $url
    fi
}

create() {
    local remote="$1"
    local dir="$2"
    test "$comp" = "bellmann" || host_str="sunny@bellmann:"
    test -n "$remove_remotes" && $simul git remote rm $remote 2>/dev/null
    if test -n "$is_top"; then
        test "$(cat .disk)" = "$remote" && return
        define_remote $remote $host_str$dir
    else
        test "$(cat ../.disk)" = "$remote" && return
        if test -z "$host_str"; then
            test -d $dir/$base/.git/annex && define_remote $remote $host_str$dir/$base
        else
            ssh sunny@bellmann "ls -a $dir/$base/.git/annex 2>/dev/null" | grep -q . &&
                define_remote $remote $host_str$dir/$base
        fi
    fi
}

git remote -v | cut -f 1 | sort -u >$tmp_begin_file

if test -n "$is_top"; then
    if test "$(lpar -s?)" != "annex"; then
        lpar -s annex
    fi
else
    if test "$(lpar -s?)" != "annex-$base"; then
        lpar -s "annex-$base"
    fi
fi

create bellmann /bellmann/home/annex
create passp /media/passp-1tb-annex/annex
create pavi /media/pavi/annex
create seagate-3tb /media/seagate-3tb/annex

if test "$comp" != "linode"; then

    create blue4g-r24 /media/blue4g-r24/annex
    create cruzer /media/cruzer/annex
    create dennis-32g-1 /media/dennis-32g-1/annex
    create exthd /media/exthd/annex
    create passp-root /media/passp-1tb-root/annex
    create softsell-32g /media/softsell-32g/annex
    create transcend-4g /media/transcend-4g/annex
    create ubstud /media/ubstud-annex/annex
    create ubstud-root /media/ubstud-root/annex
    create usb32g-1 /media/usb32g-1/annex
    create usb32g-2 /media/usb32g-2/annex
    create usb32g-3 /media/usb32g-3/annex

    test -n "$remove_remotes" && $simul git remote rm linode 2>/dev/null
    if test -n "$is_top"; then
        define_remote linode sunny@sunbase.org:/home/sunny/annex
    else
        ssh sunny@sunbase.org "ls -a /home/sunny/annex/$base/.git/annex 2>/dev/null" | grep -q . &&
            define_remote linode sunny@sunbase.org:/home/sunny/annex/$base
    fi
fi

git remote -v | cut -f 1 | sort -u >$tmp_end_file
diff -U0 $tmp_begin_file $tmp_end_file | grep -E '^[+\-][^+\-]'
rm $tmp_begin_file $tmp_end_file
